//
//  CategoryExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "CategoryExtension.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation Category(Extension)

#pragma mark Init Data Method

+ (BOOL) initCategory{
	NSError *err = nil; 
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return NO;
	}
	NSMutableArray *categoryArray = [[[NSMutableArray alloc] init] autorelease];
	NSMutableDictionary *categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Nhà Hàng" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Bình Dân" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Gia Đình" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[categoryDict setObject:@"Quán Nhậu" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:5] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Chay" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Nước Ngoài" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:7] forKey:@"id"];
	[categoryDict setObject:@"Ẩm Thực Vỉa Hè" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:8] forKey:@"id"];
	[categoryDict setObject:@"Fast Food - Ăn Nhanh" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:9] forKey:@"id"];
	[categoryDict setObject:@"Café - Giải khát" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:10] forKey:@"id"];
	[categoryDict setObject:@"Bún, Phở, Hủ tiếu" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:11] forKey:@"id"];
	[categoryDict setObject:@"Nem, Nướng " forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:12] forKey:@"id"];
	[categoryDict setObject:@"Ốc, Hải Sản" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:13] forKey:@"id"];
	[categoryDict setObject:@"Món Ăn Lạ" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:14] forKey:@"id"];
	[categoryDict setObject:@"Đặt sản, Quê Hương" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:15] forKey:@"id"];
	[categoryDict setObject:@"Hải sản" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	categoryDict = [[NSMutableDictionary alloc] init];
	[categoryDict setObject:[NSNumber numberWithInt:16] forKey:@"id"];
	[categoryDict setObject:@"Lẩu" forKey:@"name"];
	[categoryArray addObject:categoryDict];
	[categoryDict release];
	
	for (int i = 0; i < [categoryArray count]; i++) {
		NSMutableDictionary *dict = [categoryArray objectAtIndex:i];
		Category *category = [NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:managedObjectContext];
		category.id = [dict objectForKey:@"id"];
		category.name = [dict objectForKey:@"name"];
	}
	[managedObjectContext save:&err];
	return YES;
}

#pragma mark Get Data Methods

+ (NSArray *) getCateories{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Post" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}
+ (Category *) getCateory:(int)categoryId{
	Category* category = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *categoryArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", categoryId];	
	@try {
		categoryArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Category" :predicate :nil :NO :managedObjectContext];
		
		if ([categoryArray count] != 0)
		{
			category = [categoryArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return category;
}

#pragma mark Search Method

+ (NSArray*)searchByName:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Category" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

@end
